// 用于显示新建获奖信息的对话框

// 下行是关闭VSCode特性识别
// ignore_for_file: non_constant_identifier_names,prefer_typing_uninitialized_variablesW, unused_local_variable, use_build_context_synchronously
// ignore_for_file: sized_box_for_whitespace
// ignore_for_file: must_be_immutable, camel_case_types

import 'package:flutter/material.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/add_award_info/dialog_addawardinfo_err.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/award_model.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/add_award_info/dropdownmenu/dropdownmenu_award_level.dart';
import 'package:project_award_statistics/utils/mobile_material_design/dialogs/add_award_info/dropdownmenu/dropdownmenu_region_level.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/load_award_info.dart';
import 'package:project_award_statistics/utils/mobile_material_design/my_button.dart';
import 'package:provider/provider.dart';

class Dialog_AddAwardInfo extends StatelessWidget {
 
  String stu_name;  // 获取必要用户名
  TextEditingController textcontroller_award_time=TextEditingController();
  TextEditingController textcontroller_award_name=TextEditingController();
  String new_region_level="";   // 获取下方从下拉选框中获取用户获得地域等级变量
  String new_award_level="";   // 获取下方从下拉选框中获取用户获得获奖等级变量
  Dialog_AddAwardInfo({
    super.key,
    required this.stu_name,
  });

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      backgroundColor: Colors.grey[350],
      content: Container(  //设置对话框显示容器
        height: 380,
        width: 500,
        child: Column(
          children: [
            Text(
              "请在下方输入您的获奖信息:",
              style: TextStyle(
                color: Colors.black,
                fontSize:17,
              ),
            ),
            
            const SizedBox(height: 20,),  //设置控件间间距

            //设置输入用户获奖名称的文本框
            
            TextField(   
              controller:textcontroller_award_name,
              decoration: InputDecoration(   //设置输入框样式
                border: OutlineInputBorder(),
                labelText: "在这里输入您的奖项名字",
                labelStyle: TextStyle(     // 设置标签提示文字颜色为黑色
                  color: Colors.black
                ),
              ),
            ),
            
            SizedBox(height: 20,),  // 设置上下控件高度间隔

            Row(   
              children: [
                Text(
                  "所获奖项年份:",
                  style: TextStyle(
                    fontSize: 15,
                  ),
                ),
                SizedBox(width: 10,),   //设定控件间间距
                Expanded(
                  child: 
                    SizedBox(
                      height: 50,
                      child: TextField(
                        controller: textcontroller_award_time,
                        decoration: InputDecoration(   //设置输入框样式
                          border: OutlineInputBorder(),
                          labelText: "在这里输入年份",
                          labelStyle: TextStyle(     // 设置标签提示文字颜色为黑色
                            color: Colors.black
                          ),
                        ),
                      ),
                    ),
                ),
              ],
            ),

            SizedBox(height: 20,),  // 设置上下控件高度间隔

            //设置输入地域等级的下拉选框
            Row(   
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "获奖的地域等级:",
                  style: TextStyle(
                    fontSize: 15,
                  ),
                ),

                DropDownMenu_Region_level(
                  new_region_level: new_region_level,
                  onInit: (first_region_level){    // 调用初始化回调函数,透传变量给奖项地域等级默认赋个初值
                    new_region_level=first_region_level;
                  },
                  onChanged: (newValue){
                    new_region_level=newValue;
                  },
                ),
              ],
            ),

            SizedBox(height: 20,),

            // 设置输入获奖等级的下拉选框
            Row(   
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "所获奖项等级:",
                  style: TextStyle(
                    fontSize: 15,
                  ),
                ),
                // SizedBox(width: 28,),   // 设定控件间间距
                DropDownMenu_Award_level(
                  onInit: (first_award_level){     // 调用初始化回调函数,透传变量给获奖等级默认赋个初值
                    new_award_level=first_award_level;
                  },
                  onChanged: (newValue){
                    new_award_level=newValue;
                  },
                ),
              ],
            ),

            SizedBox(height: 22,),  // 设置上下控件高度间隔

            // 设定"取消"与"增加"两个按钮
            Padding(   // 先通过Padding控件控制左右边距
              padding: const EdgeInsets.only(left:5.0,right:5.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    width: 96,
                    height: 40,
                    child: MyButton(
                      text: "取消", 
                      onPressed: () => Navigator.of(context).pop(),
                    ),
                  ),
                  Container(
                    width: 96,
                    height: 40,
                    child: MyButton(
                      text: "确定", 
                      onPressed:() async {   // 按下确定键后开始插入
                        // 检查用户是否输入空/时间一栏输入有误
                        if ((textcontroller_award_name.text.isEmpty || textcontroller_award_time.text.isEmpty)||(int.tryParse(textcontroller_award_time.text)==null))
                          { // 如果有误就显示输入错误的对话框
                            await showDialog(
                              context: context, 
                              builder: (context){
                                return DialogAddawardinfoErr();
                              },
                            );
                          }
                        // 定义new_award_info用于整理即将插入数据库的数据类
                        else 
                          {
                            award_model new_award_info=award_model(
                              award_time: textcontroller_award_time.text, 
                              award_name: textcontroller_award_name.text, 
                              region_level: new_region_level, 
                              award_level: new_award_level,
                            );
                            DBProvider_AwardInfo database = DBProvider_AwardInfo();
                            await context.read<DBProvider_AwardInfo>().insert_new_award(new_award_info, stu_name);   //在监听对象眼皮底下进行插入获奖信息对象操作
                            Navigator.of(context).pop();
                          }
                      },
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
